# -*- coding: utf-8 -*-
import math
from typing import List


def py_bool():
    # if判断None和空字符串都是False
    flag = None
    if not flag:
        print('None')
    flag = ''
    if not flag:
        print('len(str) == 0')

    pass


def py_tuple():
    """
    元组:tuple
    (val1, val2, ...) = val1, val2, ...
    元组不能修改元素的个数和位置，但可以修改元素的值
    """
    t = (1, 2, 3)
    # 占位符
    a, b, c, *_ = t
    print(type(t))
    print('t =', t)

    # 常量不可改变
    t0 = 1, 2, 3
    print('== ', t == t0)
    print('is ', t is t0)

    # (1)和(1,)的区别
    i1 = (1)  # noqa
    print(type(i1))  # int
    t1 = (1,)
    print(type(t1))  # tuple
    pass


def py_list():
    """列表"""
    l: List[str] = ['A1', 'B2', 'C3']
    print(type(l))
    print('l :', l)

    l.append('D4')  # 末尾添加
    l.append('D4')  # 末尾添加
    l.remove('D4')  # 删除第一个相同的元素
    l.insert(0, 'E5')  # 指定位置插入，下标从0开始
    i = l.pop(0)  # 末尾删除或指定位置删除
    print('i :', i)

    l.sort()  # 列表元素排序
    print('sort :', l)
    l.reverse()  # 列表元素反转
    print('reverse :', l)

    l1 = [1, 2, 3]
    l2 = [2, 4, 6]

    l1.extend(l2)  # 末尾添加列表
    print('extend :', l1)
    l3 = l1 + l2  # 两个列表连接
    print('l3 :', l3)

    # 列表解析式
    # 交集
    l4 = [i for i in l1 if i in l2]
    print('l4 :', l4)
    # 差集
    l5 = [i for i in l1 if i not in l2]
    print('l5 :', l5)

    pass


def py_set():
    """集合"""
    s: set = {1, 2, 3}
    print(type(s))
    print('s =', s)
    # set是无序的，无法使用[i]访问元素

    s.add(1)
    s.add('4')
    s.update({'5', '6'})
    # s.remove('9')  # KeyError
    s.discard('9')

    s1 = {1, 2, 3}
    s2 = {2, 4, 6}

    # 并集
    # s3 = s1 | s2
    s3 = s1.union(s2)
    print('union:s3 =', s3)
    # 交集
    # s4 = s1 & s2
    s4 = s1.intersection(s2)
    print('intersection:s4 =', s4)
    # 差集
    # s5 = s1 - s2
    s5 = s1.difference(s2)
    print('difference:s5 =', s5)
    # 异或
    # s6 = s1 ^ s2
    s6 = s1.symmetric_difference(s2)
    print('s6 =', s6)
    pass


def py_dict():
    """
    字典又称键值对，默认键为字符串
    判断是否存在
    key0 in dict0 # True
    key1 in dict0 # False
    """
    d: dict = {'a': 1, 'b': 2, 'c': 3}
    print(type(d))
    print('d =', d)

    # 根据键获取值
    k = 'a'
    v = d[k]
    v = d.get(k)
    print('v :', v)
    """
    如果字典中不存在键
    dict0[key1]报KeyError异常
    dict0.get(key1)返回None
    """
    k = 'd'
    try:
        print(d[k])
    except Exception as e:
        print(type(e))
        pass
    print(d.get(k))

    key_list = ['a', 'b']

    # 获取所有键和值
    for key in d.keys():
        print('key :', key)
        pass
    for value in d.values():
        print('value :', value)
        pass
    for key, value in d.items():  # 遍历
        print(str(key) + ':' + str(value))
        pass

    # 新增键值对
    d['d'] = 4
    d.setdefault('key', 'value')

    # 删除键值对
    var = d.pop('d')

    # 将一个dict合并到另一个dict
    dict_data = {
        'e': 5,
        'f': 6,
    }
    d.update(dict_data)
    d.update({1: 'a'})
    print(d)

    # 清空
    d.clear()  # 清空
    print(d)

    pass


if __name__ == '__main__':
    """Python语言数据类型"""

    # py_bool()
    # py_tuple()
    # py_list()
    py_set()
    # py_dict()

    pass
